package com.dc.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dc.common.DateUtil;
import com.dc.common.ResponseResultVO;
import com.dc.config.InfluxdbUtils;
import com.dc.entity.MessageLog;
import com.dc.entity.QueryCondition;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.dto.Query;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RequestMapping("/orderPayment")
@RestController
@Slf4j
public class Controller {
    @Resource
    private InfluxDBMapper influxMapper;
    @Resource
    InfluxdbUtils influxdbUtils;

    @PostMapping("batchSave")
    public Object batchSave() {
        long l = System.currentTimeMillis();
        List<MessageLog> objects = new ArrayList<>();
        for (int j = 0; j < 1; j++) {
            for (int i = 0; i < 80000; i++) {
                MessageLog messageLog = new MessageLog();
                messageLog.setDeviceId(String.valueOf(i));
                messageLog.setData(String.valueOf(i));
                messageLog.setBatchId(String.valueOf(i));
                messageLog.setCountData("0");
                objects.add(messageLog);
            }
            influxdbUtils.insertBatchByRecords(objects);
        }
        long l1 = System.currentTimeMillis();

        System.out.println("最终耗时:" + (l1 - l));
        return "最终耗时:" + (l1 - l);
    }

    @PostMapping("save")
    public Object save(@RequestBody MessageLog product) {
        influxMapper.save(product);
        return "200";
    }

    @GetMapping("queryAll")
    public ResponseResultVO queryAll() {
        String sql = "select * from message_log tz('Asia/Shanghai')";
        Query query = new Query(sql);
        List<MessageLog> query1 = influxMapper.query(query, MessageLog.class);
        return ResponseResultVO.successResult(query1);
    }


    @GetMapping("queryById")
    public List<MessageLog> queryByBu(String id) {
        // tag索引的值需要加单引号
        String sql = "select * from message_log where batchId=" + "'" + id + "'" + " tz('Asia/Shanghai')";
        Query query = new Query(sql);
        List<MessageLog> products = influxMapper.query(query, MessageLog.class);
        return products;
    }


    /**
     * 分页查询
     */
    @PostMapping("/queryAllByPage")
    public ResponseResultVO queryAllByPage(@RequestBody QueryCondition ro) {
        int pageSize = 10;
        // InfluxDB支持分页查询,因此可以设置分页查询条件
        String pageQuery = " LIMIT " + pageSize + " OFFSET " + (ro.getCurrentPage() - 1) * ro.getPageSize();
        //1644659077s 带s的时间戳查询
        Date startTime = DateUtil.parseStrToDate(ro.getStartTime(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date endTime = DateUtil.parseStrToDate(ro.getEndTime(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        String queryCondition = " where time >" + (int)(startTime.getTime()/1000) + "s" + " and time <" + (int)(endTime.getTime()/1000) + "s";  //查询条件暂且为空
        // 此处查询所有内容,如果
        String queryCmd = "SELECT * FROM "
                // 查询指定设备下的日志信息
                // 要指定从 RetentionPolicyName.measurement中查询指定数据,默认的策略可以不加；
                // + 策略name + "." + measurement
                + "message_log"
                // 添加查询条件(注意查询条件选择tag值,选择field数值会严重拖慢查询速度)
                + queryCondition
                // 查询结果需要按照时间排序
                + " ORDER BY time DESC"
                // 添加分页查询条件
                + pageQuery
                + " tz('Asia/Shanghai')";

        List<MessageLog> query = influxMapper.query(new Query(queryCmd), MessageLog.class);
        query.stream().forEach(s->{
            s.setTime(DateUtil.formatDate(s.getTime()));
        });
        String count = "SELECT count(*)  FROM "
                // 查询指定设备下的日志信息
                // 要指定从 RetentionPolicyName.measurement中查询指定数据,默认的策略可以不加；
                // + 策略name + "." + measurement
                + "message_log"
                // 添加查询条件(注意查询条件选择tag值,选择field数值会严重拖慢查询速度)
                + queryCondition;

        List<MessageLog> query1 = influxMapper.query(new Query(count), MessageLog.class);
        Map<String, Object> map = new HashMap<>();
        map.put("list", query);
        map.put("count", 0);
        if (CollectionUtils.isNotEmpty(query1)) {
            String countData = query1.get(0).getCountData();
            map.put("count", Integer.parseInt(countData.substring(0, countData.indexOf("."))));
        }
        map.put("pageSize", ro.getPageSize());
        map.put("currentPage", ro.getCurrentPage());
        return ResponseResultVO.successResult(map);
    }


}
